package com.gyf.bos.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

    private T t;

    BaseAction(){

        //1.获得父类【BaseDaoImpl<T>】的参数化类型
        ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();

        //2.获得父类的参数化类型的数组
        Type[] types = genericSuperclass.getActualTypeArguments();

        //3.获取泛型类第一个参数化的类型
        Class<T> clz = (Class<T>) types[0];
        try {
            t = clz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    @Override
    public T getModel() {
        return t;
    }
    public abstract String save();

    public abstract String update();

    public abstract String delete();

    public abstract String list();
}
